package com.flanz.apache.velocity.uimocker;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.lang.NumberUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * StartUpConfig.
 * 
 * @author cf,minsth
 * 
 */
@SuppressWarnings("deprecation")
public class StartUpConfig {
	private final Logger LOG = LoggerFactory.getLogger(StartUpConfig.class);

	public final static String PROJECT_DIR = "project_dir";
	public final static String PROJECT_DOCROOT = "docroot_dir";
	public final static String PROJECT_GLOBALMACROS_FILELIST = "globalmacros_filelist";
	public final static String SERVER_PORT = "server_port";
	public final static String MAX_REQUEST_THREADS = "max_request_threads";
	public final static String MIME_MAPPING = "mime_mapping";
	public final static String APP_VARS = "app_vars";

	public final static int SERVER_PORT_DEFAULT = 8888;
	private final Configuration setting;

	StartUpConfig(final String[] args) {
		String configFileName;
		if (args != null && args.length > 0) {
			configFileName = args[0];
		} else {
			configFileName = "demo/demo.properties";
			LOG.info("startup configuration was not provided. using demo");
		}
		setting = ConfigurationProvider.getInstance(configFileName)
				.getConfiguration();

	}

	public String getString(final String key, final String fallBack) {
		String result = fallBack;
		result = setting.getString(key, fallBack);
		return result;
	}

	public String getMandatoryString(final String key) {
		String result = getString(key, null);
		if (StringUtils.isBlank(result)) {
			throw new IllegalArgumentException("Setting is mandatory '" + key
					+ "'");
		}
		return result;
	}

	public int getInteger(final String key, final int fallBack) {
		int result = fallBack;
		String string = getString(key, null);
		if (string != null) {

			result = NumberUtils.createInteger(string);
		}
		return result;

	}
}
